﻿Imports WatiN.Core
Imports WatiN.Core.DialogHandlers
Imports System
Imports System.IO
Imports DRO.connections.connections_admin
Imports DRO.DB.DB
Imports DRO
Public Class Form1
    Dim entry_text As String


    Dim ie As IE
    Dim makes As SortedList
    Dim record As New SortedList
    Dim price As String
    Dim the_date As String
    Dim time_start As DateTime
    Dim time_stop As DateTime
    Dim time_elapsed_sec As Single
    Dim time_elapsed_min As Single
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        get_connection_name("clist")
        clear("clist")
        makes = New SortedList
        defines = New SortedList
        load_list(makes, "makes", "make_name")
        load_list(defines, "definitions", "word", "word_type")

        fetch_web_page()
        ie.WaitForComplete()
        time_start = Now
        'startup_timer.Start()
        Dim sg As sql_generator
        sg = New sql_generator
        sg.verb = "SELECT"
        sg.add_list("entry_words")
        gm = New GridManager(sg, dgv_entry)
        gm.show()
    End Sub
    Private Sub btn_read_a_page_Click(sender As System.Object, e As System.EventArgs) Handles btn_read_a_page.Click
        gather_web_page()
        Dim link As Link

        link = ie.Link(Find.ByText("next 100 postings"))
        Debug.Print(link.Exists)
        If link.Exists Then
            link.Click()
        Else
            GoTo all_done
        End If
        ie.WaitForComplete()
        'Stop
all_done:
        show_time()
    End Sub

    Private Sub startup_timer_Tick(sender As System.Object, e As System.EventArgs) Handles startup_timer.Tick
        startup_timer.Stop()
        Do
            gather_web_page()
            Dim link As Link

            link = ie.Link(Find.ByText("next 100 postings"))
            Debug.Print(link.Exists)
            If link.Exists Then
                link.Click()
            Else
                Exit Do
            End If
            ie.WaitForComplete()
            'Stop

            show_time()
        Loop
        'Stop
        show_time()
    End Sub
    Function get_next_page()
        Dim result As Integer
        result = -1
        Dim link As Link

        link = ie.Link(Find.ByText("next 100 postings"))
        Debug.Print(link.Exists)
        If link.Exists Then
            link.Click()
        Else
            result = -2
            GoTo all_done
        End If
        ie.WaitForComplete()
        result = +1
        'Stop

        show_time()
all_done:
        Return result
    End Function
    Sub show_time()
        time_stop = Now
        time_elapsed_sec = DateDiff("s", time_start, time_stop)
        time_elapsed_min = time_elapsed_sec / 60
        Debug.Print("load: time_elapsed=" & time_elapsed_sec & " sec, = " & time_elapsed_min & " min")
    End Sub
    Sub fetch_web_page()
        ie = New IE("http://craigslist.com")
        Dim link As Link
        link = ie.Link(Find.ByText("cars+trucks"))
        link.Click()
        link = ie.Link(Find.ByText("BY-OWNER ONLY"))
        link.Click()

    End Sub
    Dim elem As element
    Dim exists As Boolean
    Dim n_table As Integer
    Dim parent_elem As Element
    Dim class_name As String
    Dim e_Count As Integer
    Dim elems As ElementCollection
    Dim elem_name As String


    Sub gather_web_page()
        'Dim table As Table



        n_table = ie.Tables.Count
        Dim o As Object
        o = ie.Elements
        Dim n_element As Integer
        '-------------------------------
        'this takes forever to run
        'n_element = ie.Elements.Count
        '-------------------------------
        n_element = 10000
        Dim i As Integer


        elems = ie.Elements


        i = 0
        '-----------------------------------------
        'get the date at the top of the page
        '-----------------------------------------
        elem = ie.Element(Find.ByClass("ban"))
        If elem.Exists Then
            the_date = elem.Text
        End If

        'elem = ie.Element(Find.ByText("$4400"))
        'exists = elem.Exists

        '-----------------------------------------
        'Debug.Print("exists=" & exists)


        Do While i < n_element

            Debug.Print("gather_web_page: i = " & i)
            Dim res As Integer
            res = read_one_entry(i)
            Application.DoEvents()
            If res < 0 Then
                'Stop
                If res = -2 Then
                    'no more elements
                    Dim more_pages As Integer
                    more_pages = get_next_page()
                    If more_pages < 0 Then
                        Debug.Print("gather_web_page: NO MORE PAGES")
                        Exit Do
                    Else
                        set_web_page()
                    End If
                Else
                    Stop
                End If
            End If
        Loop
        elem = ie.Element(Find.By("tagname", "row"))


    End Sub
    Private Sub btn_set_web_page_Click(sender As System.Object, e As System.EventArgs) Handles btn_set_web_page.Click
        set_web_page()
    End Sub
    Sub set_web_page()
        'Dim table As Table

        n_table = ie.Tables.Count
        Dim o As Object
        o = ie.Elements
        Dim n_element As Integer
        '-------------------------------
        'this takes forever to run
        'n_element = ie.Elements.Count
        '-------------------------------
        n_element = 10000

        elems = ie.Elements

        e_Count = 0
        '-----------------------------------------
        'get the date at the top of the page
        '-----------------------------------------
        elem = ie.Element(Find.ByClass("ban"))
        If elem.Exists Then
            the_date = elem.Text
        End If

        'elem = ie.Element(Find.ByText("$4400"))
        'exists = elem.Exists

        '-----------------------------------------
        'Debug.Print("exists=" & exists)


        'Do While e_Count < n_element

        '    Debug.Print("gather_web_page: i = " & e_Count)
        '    read_one_entry(i)
        'Loop
        'elem = ie.Element(Find.By("tagname", "row"))


    End Sub
    Private Sub btn_read_an_entry_Click(sender As System.Object, e As System.EventArgs) Handles btn_read_an_entry.Click
        Dim res As Integer
        res = read_one_entry(e_Count)
        If res = -2 Then
            'no more elements
            Dim more_pages As Integer
            more_pages = get_next_page()
            If more_pages < 0 Then
                MsgBox("NO MORE PAGES")
            Else
                set_web_page()
            End If

        End If
    End Sub
    Function read_one_entry(ByRef i As Integer)
        Dim result As Integer
        result = -1
        Do
            Try
                elem = elems(e_Count)
            Catch ex As Exception
                result = -2 'we've exhausted all the elements
                'Stop
                GoTo all_done
            End Try

            Dim parent As Element
            Dim class_name As String
            parent = elem.Parent
            elem_name = elems(e_Count).IdOrName
            class_name = elems(e_Count).ClassName
            If class_name = "row" Then
                Debug.Print("--------------------")
            End If
            Debug.Print(e_Count & " " & class_name & " -- " & elem_name)

            If class_name = "row" Then
                stp = New Stopwatch
                stp.Start()
                Dim ta As String
                Dim tb As String
                entry_text = elem.Text
                ta = elem.TextAfter
                tb = elem.TextBefore
                show_element(e_Count, elems)
                write_entry()
                stp.Stop()
                Dim elapsed
                elapsed = stp.ElapsedMilliseconds
                txt_elapsed.Text = elapsed
                result = +1
                GoTo all_done
            End If
            'Stop
            e_Count += 1
        Loop
all_done:
        Return result
    End Function
    Sub show_element(ByRef i As Integer, ByVal elems As ElementCollection)

        Dim elem As Element
        Dim elem_name As String
        Dim class_name As String
        Dim j As Integer
        record = New SortedList
        record.Add("elem0", entry_text)
        For j = 1 To 10
            If elems(i + 1).ClassName = "row" Or elems(i + 1).ClassName = "ban" Then
                Exit For
            End If
            Dim text As String
            If elems(i + 1).Text Is Nothing Then
                text = ""
            Else
                text = elems(i + 1).Text
            End If

            If text.Contains("next 100") Then
                Exit For
            End If
            If text.Contains("Copyright ") Then
                Exit For
            End If
            i = i + 1
            elem_name = elems(i).IdOrName
            class_name = elems(i).ClassName
            elem = elems(i)
            Dim a_link As WatiN.Core.Link
            Dim label As String
            Dim url As Object
            url = elem.GetType().ToString.ToLower
            price = ""
            If url.contains("link") Then
                a_link = CType(elem, WatiN.Core.Link)
                url = a_link.Url
                record.Add("url", url)
                price = a_link.TextAfter
                price = price.Replace("-", "")
                price = price.Trim
                record.Add("price", price)
            End If

            label = "elem" & j
            Dim key_value As String
            key_value = elems(i).Text
            record.Add(label, key_value)
            Debug.Print(i & " " & label & " " & class_name & " -- " & elem_name)
            Debug.Print(i & " " & label & " " & elems(i).Text)
            'If j > 7 Then Stop
        Next

    End Sub

    Sub write_entry()
        Dim inner_html As String
        Dim model As String
        Dim loc As String
        Dim sg As New sql_generator(gCC)

        inner_html = record("elem0")
        model = record("elem2")
        loc = record("elem3")
        price = record("price")
        sg.verb = "INSERT"
        sg.add_list("clist")
        sg.add_insert_value("ad_date", the_date)
        sg.add_insert_value("price", price)
        sg.add_insert_value("inner_html", inner_html)
        sg.add_insert_value("model_line", model)
        sg.add_insert_value("location", loc)
        Dim k As Integer
        k = sg.update_database
        If k < 0 Then
            Stop
        End If
        process_tokens(inner_html)

        'Dim sg As sql_generator
        'sg = New sql_generator
        'sg.verb = "SELECT"
        'sg.add_list("entry_words")
        'If gm IsNot Nothing Then
        '    gm.dispose()
        'End If
        'gm = New GridManager(sg, dgv_entry)
        gm.show()

        'Stop
    End Sub



End Class
